/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Logger, PluginFileUtil } from '../../framework';
import { HMRouterPluginConfig } from '../config/HMRouterPluginConfig';
import { HMRouterExtensionContext } from '../HMRouterExtensionContext';

export class CompletionProcessor {
  private context: HMRouterExtensionContext;
  private config: HMRouterPluginConfig;

  constructor(context: HMRouterExtensionContext) {
    this.context = context;
    this.config = context.config;
  }

  execute(): void {
    Logger.debug(this.context.node.getNodeName(), `Start to delete temporary files...`);
    // If saveGeneratedFile is true, skip deleting
    if (this.context.config.saveGeneratedFile) {
      Logger.info(this.context.node.getNodeName(), `Save generated file is true, skip deleting temporary files`);
      return;
    }

    // 1. Delete the router map file
    this.deleteRouterMapFile();
    // 2. Delete the original file
    this.deleteRawFile();
    // 3. Delete the generated file
    this.deleteGeneratorFile();
    // 4. If obfuscation is enabled, delete the obfuscation configuration file
    if (this.config?.autoObfuscation) {
      this.deleteObfuscationFile();
    }
  }

  /**
   * Delete the router map file
   */
  private deleteRouterMapFile(): void {
    let routerMapDirPath = this.config.getRouterMapDir();
    // If the path exists, delete the hm_router_map.json file
    if (PluginFileUtil.exist(routerMapDirPath)) {
      PluginFileUtil.unlinkSync(routerMapDirPath);
      Logger.info(
        this.context.node.getNodeName(),
        `Delete router map file successfully, filePath: ${routerMapDirPath}`
      );
    }
  }

  /**
   * Delete the original file
   */
  private deleteRawFile(): void {
    let rawFilePath = this.config.getRawFilePath();
    if (PluginFileUtil.exist(rawFilePath)) {
      PluginFileUtil.unlinkSync(rawFilePath);
      Logger.info(this.context.node.getNodeName(), `Delete raw file successfully, filePath: ${rawFilePath}`);
    }
  }

  /**
   * Delete the generated file
   */
  private deleteGeneratorFile(): void {
    // Get the builderDirPath path
    let builderDirPath = this.config.getBuilderDir();
    // If the path exists, delete the builder directory
    if (PluginFileUtil.exist(builderDirPath)) {
      PluginFileUtil.rmSync(builderDirPath, {
        recursive: true,
      });
      Logger.info(this.context.node.getNodeName(), `Delete generator file successfully, filePath: ${builderDirPath}`);
    }
  }

  /**
   * Delete the obfuscation configuration file
   */
  private deleteObfuscationFile(): void {
    let obfuscationFilePath = this.config.getObfuscationFilePath();
    if (PluginFileUtil.exist(obfuscationFilePath)) {
      PluginFileUtil.unlinkSync(obfuscationFilePath);
      Logger.info(
        this.context.node.getNodeName(),
        `Delete obfuscation file successfully, filePath: ${obfuscationFilePath}`
      );
    }
    let consumerRulesPath = this.config.getConsumerRulesFilePath();
    if (PluginFileUtil.exist(consumerRulesPath)) {
      PluginFileUtil.unlinkSync(consumerRulesPath);
      Logger.info(
        this.context.node.getNodeName(),
        `Delete consumer rules file successfully, filePath: ${consumerRulesPath}`
      );
    }
  }
}
